﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zhuang.Model.EF;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Zhuang.DAL;

namespace Zhuang.WebSite.BLL.SystemManagement
{
    public class Sys_RoleBLL
    {

        Sys_RoleDAL sys_RoleDAL = new Sys_RoleDAL();

        public DataTable GetPageOfRole(int page, int pageSize, ref int totalRowCount, string conditions)
        {
            return sys_RoleDAL.GetPage(page,pageSize,ref totalRowCount,conditions);
        }

        public Sys_Role GetEntity(int id)
        {
            Sys_Role role = null;

            DataTable dt = sys_RoleDAL.Get(id);

            if (dt.Rows.Count > 0)
            {
                role = new Sys_Role();
                role.RoleId = Convert.ToInt32(dt.Rows[0]["roleId"]);
                role.RoleName = dt.Rows[0]["roleName"].ToString();
                role.Description = dt.Rows[0]["description"].ToString();
                role.CreationDate = DateTime.Parse(dt.Rows[0]["creationDate"].ToString());
                role.ModifiedDate = DateTime.Parse(dt.Rows[0]["modifiedDate"].ToString());
            }

            return role;
        }


        public int UpdateEntity(Sys_Role role)
        {
            return sys_RoleDAL.Update(role.RoleId,role.Description) ;
        }


        public int DeleteEntity(int id)
        {
            return sys_RoleDAL.Delete(id);
        }


        public int AddEntity(Sys_Role role)
        {

            if (sys_RoleDAL.ExistsRoleName(role.RoleName))
            {
               
                return sys_RoleDAL.Add(role.RoleName,role.Description);
            }
            else
            {
                return 0;
            }

        }

        public bool SaveRolePermission(int roleId, DataTable dtMenus, DataTable dtButtons)
        {
            return sys_RoleDAL.SaveRolePermission(roleId,dtMenus,dtButtons);
        }


        public DataTable GetAllRoles()
        {
            return sys_RoleDAL.GetAllRoles();
        }
    }
}
